package com.ajavaer.framework.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.Resource;

@ConfigurationProperties(prefix = "ajavaer.weixin.sns")
public class WeixinSnsConfig {

    @Resource
    WeixinPublicConfig weixinPublicConfig;

    private String sns_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
    private String sns_code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
    private String check_sns_access_token_url = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
    private String refresh_token_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=%s&refresh_token=REFRESH_TOKEN";
    private String userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";

    public String getSns_code_url(String redirect_uri, String scope, String state) {
        return String.format(sns_code_url, weixinPublicConfig.getAppid(), redirect_uri, scope, state);
    }

    public String getSns_access_token_url(String code) {
        return String.format(sns_access_token_url, weixinPublicConfig.getAppid(), weixinPublicConfig.getSecret(), code);
    }

    public String getCheck_sns_access_token_url(String access_token, String openid) {
        return String.format(check_sns_access_token_url, access_token, openid);
    }

    public String getRefresh_token_url(String grant_type) {
        return String.format(refresh_token_url, weixinPublicConfig.getAppid(), grant_type);
    }

    public String getUserinfo_url(String access_token, String openid) {
        return String.format(userinfo_url, access_token, openid);
    }

}
